<?php
namespace app\manage\validate;
use think\Validate;

class BaseValidate extends Validate
{
    // 是否使用true基础的验证规则
    protected $useBaseRules = true;
    // 基础的验证规则
    protected $baseRule = [
        'id' => ['integer', 'egt' => 0],
        'pid' => ['integer', 'egt' => 0],
        'parent_id' => ['integer', 'egt' => 0],
        'level' => ['integer', 'between' => '0,100'],
        'status' => ['integer'],
        'order' => ['integer'],
        'page' => ['integer', 'gt' => 0],
        'listRows' => ['integer', 'between'=>'1,100'],
        'list_rows' => ['integer', 'between'=>'1,100'],
        'phone'=> ['regex'=>'^1\d{10}'],
        'sex' => ['in' => 'gent,lady,no'],
        'token' => ['alphaNum', 'length' => '32'],
        'app_key' => ['alphaNum', 'length' => '32']
    ];
    // 基础的验证提示消息
    protected $baseMessage = [
        'id' => 'ID必须是大于等于0的整数',
        'pid' => '父ID必须是大于等于0的整数',
        'parent_id' => '父ID必须是大于等于0的整数',
        'level.integer' => '层级必须是整数',
        'level.between' => '层级必须是0-100的整数',
        'status.integer' => 'status状态必须是整数',
        'order.integer' => 'order排序必须是整数',
    ];
    // 基础的验证字段
    protected $baseField = [];
    /**
     * 构造函数
     * @access public
     * @param array $rules 验证规则
     * @param array $message 验证提示信息
     * @param array $field 验证字段描述信息
     */
    public function __construct(array $rules = [], $message = [], $field = [])
    {
        parent::__construct($rules, $message, $field);
        // 如果使用基础验证规则
        if ($this->useBaseRules) {
            $this->rule    = array_merge($this->baseRule, $this->rule);
            $this->message = array_merge($this->baseMessage, $this->message);
            $this->field   = array_merge($this->baseField, $this->field); 
        }
    }
}
